const express = require('express');
const router = express.Router();
const mysql = require("../../../mysql/db");


router.post('/', (req, res) => {
    let body = req.body
    let pageSize = body.limit || 10; //分页参数
    let currenPage = body.currenPage || 1; //当前页码
    let startRow = (currenPage - 1) * pageSize;
    switch (body.name) {
        case 'today':
            mysql.query("select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where DATE_FORMAT(datetime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d')  ORDER BY datetime desc  limit ?, ?", [startRow, pageSize], (err, data) => {
                if (err) {
                    throw err;
                }
                mysql.query("select sum(b.count) as count from (select count(1) as count from (select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where DATE_FORMAT(datetime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d')  ORDER BY datetime desc) a GROUP BY  date_format( a.datetime,'%Y-%m-%d')) b", (err, count) => {
                    res.send({ count: count[0].count, data })
                })
            })
            break;
        case 'whole':
            mysql.query("select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log`  ORDER BY datetime desc limit ?, ?", [startRow, pageSize], (err, data) => {
                if (err) {
                    throw err;
                }
                mysql.query("select sum(b.count) as count from (select count(1) as count from (select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log`  ORDER BY datetime desc) a GROUP BY  date_format( a.datetime,'%Y-%m-%d')) b", (err, count) => {
                    res.send({ count: count[0].count, data })
                })
            })
            break;
        case 'week':
            mysql.query("select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where YEARWEEK( date_format( datetime,'%Y-%m-%d'),1 ) = YEARWEEK(now(),1) ORDER BY datetime desc limit ?, ?", [startRow, pageSize], (err, data) => {
                if (err) {
                    throw err;
                }

                mysql.query("select sum(b.count) as count from (select count(1) as count from (select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where YEARWEEK( date_format( datetime,'%Y-%m-%d'),1 ) = YEARWEEK(now(),1) ORDER BY datetime desc) a GROUP BY  date_format( a.datetime,'%Y-%m-%d')) b", (err, count) => {
                    res.send({ count: count[0].count, data })
                })

            })
            break;
        case 'month':
            mysql.query("select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where DATE_FORMAT(datetime, '%Y-%m' ) = DATE_FORMAT( CURDATE() , '%Y-%m' )  ORDER BY datetime desc limit ?, ?", [startRow, pageSize], (err, data) => {
                if (err) {
                    throw err;
                }
                mysql.query("select sum(b.count) as count from (select count(1) as count from (select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where DATE_FORMAT(datetime, '%Y-%m' ) = DATE_FORMAT( CURDATE() , '%Y-%m' )  ORDER BY datetime desc) a GROUP BY  date_format( a.datetime,'%Y-%m-%d')) b", (err, count) => {
                    res.send({ count: count[0].count, data })
                })
            })
            break;
            // default:
            //     mysql.query("select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where DATE_FORMAT(datetime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d')  ORDER BY datetime desc  limit ?, ?", [startRow, pageSize], (err, data) => {
            //         if (err) {
            //             throw err;
            //         }
            //         mysql.query("select sum(b.count) as count from (select count(1) as count from (select username,reason,typevalue,DATE_FORMAT(datetime,'%Y-%m-%d %H:%i:%s') as datetime from  `general_log` where DATE_FORMAT(datetime,'%Y-%m-%d')=DATE_FORMAT(now(),'%Y-%m-%d')  ORDER BY datetime desc) a GROUP BY  date_format( a.datetime,'%Y-%m-%d')) b", (err, count) => {
            //             res.send({ count: count[0].count, data })
            //         })
            //     })

    }

})

module.exports = router